package cn.jhz.learn.blog.test.impl;

import cn.jhz.learn.blog.component.RequestHolder;
import cn.jhz.learn.blog.dao.other.BlogCommentMapper;
import cn.jhz.learn.blog.po.other.BlogComment;
import cn.jhz.learn.blog.service.core.open.*;
import cn.jhz.learn.blog.service.core.open.article.ArticleService;
import cn.jhz.learn.blog.service.core.open.photo.PhotoService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Optional;

@Validated
@Service("commentService")
public class CommentServiceImpl implements CommentService {
	private final BlogCommentMapper commentMapper;
	private final ArticleService articleService;
	private final PhotoService photoService;
	private final EssayService essayService;
	private final GuestbookService guestbookService;

//	@Autowired
	public CommentServiceImpl(BlogCommentMapper commentMapper, ArticleService articleService,
							  PhotoService photoService, EssayService essayService, GuestbookService guestbookService) {
		this.commentMapper = commentMapper;
		this.articleService = articleService;
		this.photoService = photoService;
		this.essayService = essayService;
		this.guestbookService = guestbookService;
	}

	@Override
	public Optional<BlogComment> publishArticleComment(BlogComment newComment) {
		articleService.checkExist(newComment.getEntityId());
		return publishComment(newComment, "Blog_Article_comment");
	}

	@Override
	public Optional<BlogComment> publishPhotoComment(BlogComment newComment) {
		photoService.checkExist(newComment.getEntityId());
		return publishComment(newComment, "Blog_Photo_comment");
	}

	@Override
	public Optional<BlogComment> publishEssayComment(BlogComment newComment) {
		essayService.checkExist(newComment.getEntityId());
		return publishComment(newComment, "blog_essay_comment");
	}

	@Override
	public Optional<BlogComment> publishGuestbookComment(BlogComment newComment) {
		guestbookService.checkExist(newComment.getEntityId());
		return publishComment(newComment, "blog_guestbook_comment");
	}

	@Override
	public Optional<Integer> removeArticleComment(Integer commentId) {
		return removeComment(commentId, "blog_article_comment");
	}

	@Override
	public Optional<Integer> removePhotoComment(Integer commentId) {
		return removeComment(commentId, "blog_photo_comment");
	}

	@Override
	public Optional<Integer> removeEssayComment(Integer commentId) {
		return removeComment(commentId, "blog_essay_comment");
	}

	@Override
	public Optional<Integer> removeGuestbookComment(Integer commentId) {
		return removeComment(commentId, "blog_guestbook_comment");
	}

	private Optional<BlogComment> publishComment(BlogComment comment, String tableName){
		checkContent(comment.getContent());
		commentMapper.insertSelective(comment);
		return Optional.of(comment);
	}
	private Optional<Integer> removeComment(Integer id, String tableName){
		checkExist(id);
		commentMapper.updateStatusByPrimaryKey(id, (byte) 0);
		return Optional.of(id);
	}
	public void checkExist(Integer id){
		BlogComment comment = commentMapper.selectByPrimaryKey(id);
		if(comment == null)
			throw ExceptionConstant.paramCommentIsNotExist.get();
		else if(!comment.getRaterId().equals(RequestHolder.getCurrentUser().getId()) || comment.getStatus() != (byte) 1)
			throw ExceptionConstant.paramCommentIsNotExist.get();
	}

	private void checkContent(String string){

	}
}
